﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eProject_SEM3_SecurityServices.Bussiness;
using System.Data;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;

namespace eProject_SEM3_SecurityServices.DataAccess
{
    public class DCustomer
    {

       public bool AddCustomer(OCustomer customer)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
               
                new SqlParameter("@customerName",customer.CustomerName),
                new SqlParameter("@customerPhone",customer.CustomerPhone),
                new SqlParameter("@customerAddress",customer.CustomerAddress)
            };
            return DConnectSQL.ExecuteNonQuery("Addnewcustomer",CommandType.StoredProcedure,parameter);
        }

        public bool UpdateCustomer(OCustomer customer)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@customerId",customer.CustomerID),
                new SqlParameter("@customerName",customer.CustomerName),
                new SqlParameter("@customerPhone",customer.CustomerPhone),
                new SqlParameter("@customerAddress",customer.CustomerAddress)
            };
            return DConnectSQL.ExecuteNonQuery("UpdatenewCustomer", CommandType.StoredProcedure, parameter);
        }

        public bool DeleteCustomer(int customerID)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@customerId",customerID)             
            };
            return DConnectSQL.ExecuteNonQuery("DeleteCustomer", CommandType.StoredProcedure, parameter);
        }

        public OCustomer getCustomerDetails(int customerId)
        {
            OCustomer customer = null;
            SqlParameter[] paramenters = new SqlParameter[]
            {
                new SqlParameter("@cusId",customerId)
            };

            using (DataTable table = DConnectSQL.ExecuteParamerizedSelectCommand("getCustomerDetail", CommandType.StoredProcedure, paramenters))
            {
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    customer = new OCustomer();

                    customer.CustomerID = Convert.ToInt32(row["customerId"]);
                    customer.CustomerName = row["customerName"].ToString();
                    customer.CustomerPhone = row["customerPhone"].ToString();
                    customer.CustomerAddress = row["customerAddress"].ToString();
                }
            }
            return customer;
        }

        public DataTable getCustomerList()
        {
            DataTable table = DConnectSQL.ExecuteSelectCommand("GetCustomerList",CommandType.StoredProcedure);

            return table;
        }      

    }
}